<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="author" content="esy">
    
    <meta name="description" content="esy">
    
    
    
    
    
    
    <title>chatbot在python上实现 | ESY</title>
    <link href="https://esyyes.github.io" rel="prefetch" />

    
<link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/aos.css">
<link rel="stylesheet" href="/css/style.css">

    
<script src="/js/jquery.min.js"></script>

    
<script src="/js/bootstrap.min.js"></script>

    
<script src="/js/aos.js"></script>

    
<script src="/js/highslide/highslide-full.min.js"></script>

    
<link rel="stylesheet" href="/js/highslide/highslide.css">

    <style type="text/css">
        @media (max-width: 768px) {
            body {
                background-color: #f0f0f0;
                background: url('/imgs/xsbg.gif');
                background-attachment: fixed;
            }
        }
    </style>
    
    <!--<script type="text/javascript">
      if (document.images) {
        var avatar = new Image();
        avatar.src = '/imgs/avatar.jpg'
        var previews = 'preview1.jpg,preview2.jpg,preview3.jpg,preview4.jpg'.split(',')
        var previewsPreLoad = []
        for(var i = 0; i < length; i++) {
          previewsPreLoad.push(new Image())
          previewsPreLoad[previewsPreLoad.length - 1].src = '/imgs/preview' + previews[i]
        }
      }
    </script>-->
<meta name="generator" content="Hexo 5.2.0"></head>
<body>
    <!-- 背景轮播图功能 -->
    <section class="hidden-xs">
    <ul class="cb-slideshow">
        <li><span>天若</span></li>
        <li><span>有情</span></li>
        <li><span>天亦老</span></li>
        <li><span>我为</span></li>
        <li><span>长者</span></li>
        <li><span>续一秒</span></li>
    </ul>
</section>
    <!-- 欧尼酱功能, 谁用谁知道 -->
    
    <div class="gal-menu gal-dropdown">
    <div class="circle" id="gal">
        <div class="ring">
            <a href="https://esyyes.github.io" class="menuItem" style="left: 50%; top: 15%;">首页</a>
            
            <a class="menuItem" style="left: 80.3109%; top: 32.5%;">下一页</a>
            
            <a href="/archives" class="menuItem" style="left: 80.3109%; top: 67.5%;">归档</a>
            <a href="/about" class="menuItem" style="left: 50%; top: 85%;">关于</a>
            <a href="/message" class="menuItem" style="left: 19.6891%; top: 67.5%;">留言板</a>

            
            <a class="menuItem" style="left: 19.6891%; top: 32.5%;">上一页</a>
            
        </div>
        <audio id="audio" src="/imgs/oni.mp3"></audio>
    </div>
</div>
    
    <header class="navbar navbar-inverse" id="gal-header">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed"
                    data-toggle="collapse" data-target=".bs-navbar-collapse"
                    aria-expanded="false">
                <span class="fa fa-lg fa-reorder"></span>
            </button>
            <a href="https://esyyes.github.io">
                
                <style>
                    #gal-header .navbar-brand {
                        height: 54px;
                        line-height: 24px;
                        font-size: 28px;
                        opacity: 1;
                        background-color: rgba(0,0,0,0);
                        text-shadow: 0 0 5px #fff,0 0 10px #fff,0 0 15px #fff,0 0 20px #228DFF,0 0 35px #228DFF,0 0 40px #228DFF,0 0 50px #228DFF,0 0 75px #228DFF;
                    }
                </style>
                <!-- 这里使用文字(navbar_text or config.title) -->
                <div class="navbar-brand">ESY</div>
                
            </a>
        </div>
        <div class="collapse navbar-collapse bs-navbar-collapse">
            <ul class="nav navbar-nav" id="menu-gal">
                
                
                <li class="">
                    <a href="/">
                        <i class="fa fa-home"></i>首页
                    </a>
                </li>
                
                
                
                <li class="">
                    <a href="/archives">
                        <i class="fa fa-archive"></i>归档
                    </a>
                </li>
                
                
                
                
                <li class="dropdown">
                    <!-- TODO 添加hover dropdown效果 -->
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown"
                       aria-haspopup="true" aria-expanded="false" data-hover="dropdown">
                        <i class="fa fa-list"></i>分类
                    </a>
                    <ul class="dropdown-menu">
                        
                        
                        <li>
                            <a href="/categories/py-study/">py_study</a>
                        </li>
                        
                        <li>
                            <a href="/categories/nlp/">nlp</a>
                        </li>
                        
                        <li>
                            <a href="/categories/Graduation-work/">Graduation work</a>
                        </li>
                        
                        <li>
                            <a href="/categories/work/">work</a>
                        </li>
                        
                        <li>
                            <a href="/categories/hexo/">hexo</a>
                        </li>
                        
                        <li>
                            <a href="/categories/hexo%E5%AE%8C%E5%96%84/">-hexo完善</a>
                        </li>
                        
                        
                        <li>
                            <a href="/categories">...</a>
                        </li>
                        
                        
                    </ul>
                </li>
                
                
                
                
                
                <li class="dropdown">
                    <!-- TODO 添加hover dropdown效果 -->
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown"
                       aria-haspopup="true" aria-expanded="false" data-hover="dropdown">
                        <i class="fa fa-tags"></i>标签
                    </a>
                    <ul class="dropdown-menu">
                        
                        
                        <li>
                            <a href="/tags/py-study/">py_study</a>
                        </li>
                        
                        <li>
                            <a href="/tags/nlp/">nlp</a>
                        </li>
                        
                        <li>
                            <a href="/tags/Graduation-work/">Graduation work</a>
                        </li>
                        
                        <li>
                            <a href="/tags/work/">work</a>
                        </li>
                        
                        <li>
                            <a href="/tags/hexo/">hexo</a>
                        </li>
                        
                        <li>
                            <a href="/tags/%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/">-个人博客搭建</a>
                        </li>
                        
                        
                        <li>
                            <a href="/tags">...</a>
                        </li>
                        
                        
                    </ul>
                </li>
                
                
                
                
                <li class="">
                    <a href="/about">
                        <i class="fa fa-user"></i>关于我
                    </a>
                </li>
                
                
            </ul>
        </div>
    </div>
</header>
    <div id="gal-body">
        <div class="container">
            <div class="row">
                <div class="col-md-8 gal-right" id="mainstay">
                    
<article class="article well article-body" id="article">
    <div class="breadcrumb">
        <i class="fa fa-home"></i>
        <a href="https://esyyes.github.io">ESY</a>
        >
        <span>chatbot在python上实现</span>
    </div>
    <!-- 大型设备详细文章 -->
    <div class="hidden-xs">
        <div class="title-article">
            <h1>
                <a href="/2020/07/20/python%20work/chatbot%E5%9C%A8python%E4%B8%8A%E5%AE%9E%E7%8E%B0/">chatbot在python上实现</a>
            </h1>
        </div>
        <div class="tag-article">
            
            <span class="label label-gal">
                <i class="fa fa-tags"></i>
                
                <a href="/tags/work/">work</a>
                
            </span>
            
            <span class="label label-gal">
                <i class="fa fa-calendar"></i> 2020-07-20
            </span>
            
        </div>
    </div>
    <!-- 小型设备详细文章 -->
    <div class="visible-xs">
        <center>
            <div class="title-article">
                <h4>
                    <a href="/2020/07/20/python%20work/chatbot%E5%9C%A8python%E4%B8%8A%E5%AE%9E%E7%8E%B0/">chatbot在python上实现</a>
                </h4>
            </div>
            <p>
                <i class="fa fa-calendar"></i> 2020-07-20
            </p>
            <p>
                
                <i class="fa fa-tags"></i>
                
                <a href="/tags/work/">work</a>
                
                
                
            </p>
        </center>
    </div>
    <div class="content-article">
        <h1 id="chatbot在python上实现流程"><a href="#chatbot在python上实现流程" class="headerlink" title="chatbot在python上实现流程"></a>chatbot在python上实现流程</h1><h2 id="基于规则输出简单的回复"><a href="#基于规则输出简单的回复" class="headerlink" title="基于规则输出简单的回复"></a>基于规则输出简单的回复</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    data1 = pd.read_csv(<span class="string">&#x27;Q&amp;A pairs.csv&#x27;</span>)</span><br><span class="line">    print(<span class="string">&quot;Hello, I&#x27;m a question-and-answer chatbot for the tourism domain based on retrieval mode.&quot;</span></span><br><span class="line">          <span class="string">&quot;If you want to exit, input &#x27;Bye&#x27;!&quot;</span>)</span><br><span class="line">    greeting_output = [<span class="string">&quot;hi&quot;</span>, <span class="string">&quot;hey&quot;</span>, <span class="string">&quot;hello&quot;</span>, <span class="string">&quot;I&#x27;m glad! You are talking to me&quot;</span>]</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        text2 = <span class="built_in">input</span>(<span class="string">&quot;Please enter a question: \t&quot;</span>)</span><br><span class="line">        text1 = <span class="string">&quot; &quot;</span>.join([token.lemma_ <span class="keyword">for</span> token <span class="keyword">in</span> nlp(text2)])</span><br><span class="line">        <span class="keyword">if</span> text1 == <span class="string">&#x27;hey&#x27;</span> <span class="keyword">or</span> text1 == <span class="string">&#x27;hi&#x27;</span> <span class="keyword">or</span> text1 == <span class="string">&#x27;hello&#x27;</span> <span class="keyword">or</span> text1 == <span class="string">&#x27;HI&#x27;</span>:</span><br><span class="line">            print(random.choice(greeting_output))</span><br><span class="line">        <span class="keyword">elif</span> text1 == <span class="string">&#x27;thank&#x27;</span> <span class="keyword">or</span> text1 == <span class="string">&#x27;thank -PRON-&#x27;</span> <span class="keyword">or</span> text1 == <span class="string">&#x27;THANK&#x27;</span>:</span><br><span class="line">            print(<span class="string">&#x27;You are welcome.&#x27;</span>)</span><br><span class="line">        <span class="keyword">elif</span> text1 == <span class="string">&#x27;bye&#x27;</span> <span class="keyword">or</span> text1 == <span class="string">&#x27;BYE&#x27;</span>:</span><br><span class="line">            print(<span class="string">&#x27;Bye!&#x27;</span>)</span><br><span class="line">            <span class="keyword">break</span></span><br></pre></td></tr></table></figure>

<p>重新写个简单的程序吧</p>
<h2 id="首先对问题进行预处理"><a href="#首先对问题进行预处理" class="headerlink" title="首先对问题进行预处理"></a>首先对问题进行预处理</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">del_stop</span>(<span class="params">text</span>):</span></span><br><span class="line">    token_doc = [token.lemma_ <span class="keyword">for</span> token <span class="keyword">in</span> nlp(text)]</span><br><span class="line">    <span class="comment"># 去除停用词后创建单词列表</span></span><br><span class="line">    filtered_sentence = []</span><br><span class="line">    <span class="keyword">for</span> word <span class="keyword">in</span> token_doc:</span><br><span class="line">        lexeme = nlp.vocab[word]</span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> lexeme.is_stop != <span class="literal">False</span>:</span><br><span class="line">            filtered_sentence.append(word)</span><br><span class="line">    <span class="keyword">return</span> filtered_sentence</span><br><span class="line">将字母都切换为小写，并切除停靠词，得到关键的语句</span><br></pre></td></tr></table></figure>

<h2 id="计算TF-IDF"><a href="#计算TF-IDF" class="headerlink" title="计算TF_IDF"></a>计算TF_IDF</h2><p>第一步还是将问题进行预处理，然后获取语料库中的问题的词频，得到语料库的词汇</p>
<p>第二步判断提出的问题的关键词汇是否在语料库的词汇中，如果不在就直接返回0，程序会返回一个不知道</p>
<p>第三步计算词汇的idf</p>
<p>第四步计算每个单词在每个问题中的频率</p>
<p>第五步得到每个问题的关键词汇的tf-idf的分数列表</p>
<p>第六步计算提出问题在每个问题中能得到tf-idf多少分</p>
<p>第七步将每个tf-idf的得分升序排列，然后召回最高得分的5个问题，和对应的5个答案</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line">def QA_tf_idf(data, filtered_sentence):</span><br><span class="line">    question &#x3D; np.array(data[&#39;Question&#39;]).tolist()</span><br><span class="line">    answer &#x3D; np.array(data[&#39;Answer&#39;]).tolist()</span><br><span class="line">    # 将问题进行分词</span><br><span class="line">    list_ques &#x3D; [[t.lemma_ for t in nlp(question[i])] for i in range(len(data))]</span><br><span class="line">    # 去除停用词后创建单词列表</span><br><span class="line">    list_key &#x3D; []</span><br><span class="line">    for i in range(len(data)):</span><br><span class="line">        filtered_sentence1 &#x3D; []</span><br><span class="line">        for word in list_ques[i]:</span><br><span class="line">            lexeme &#x3D; nlp.vocab[word]</span><br><span class="line">            if not lexeme.is_stop !&#x3D; False:</span><br><span class="line">                filtered_sentence1.append(word)</span><br><span class="line">        list_key.append(filtered_sentence1)</span><br><span class="line">    # 统计词频和词汇,看单词出现的次数</span><br><span class="line">    doc_frequency &#x3D; defaultdict(int)</span><br><span class="line">    list_words &#x3D; list_key</span><br><span class="line">    for word_list in list_words:</span><br><span class="line">        for i in word_list:</span><br><span class="line">            doc_frequency[i] +&#x3D; 1</span><br><span class="line">    l1 &#x3D; set(filtered_sentence)</span><br><span class="line">    l2 &#x3D; set(doc_frequency.keys())</span><br><span class="line"></span><br><span class="line">    if not l1.issubset(l2) !&#x3D; False:</span><br><span class="line">        return 0</span><br><span class="line">    else:</span><br><span class="line">        # 计算每个词的IDF值</span><br><span class="line">        word_idf &#x3D; &#123;&#125;  # 存储每个词的idf值</span><br><span class="line">        word_doc &#x3D; defaultdict(int)  # 存储包含该词的文档数</span><br><span class="line">        for i in doc_frequency:</span><br><span class="line">            for j in list_words:</span><br><span class="line">                if i in j:</span><br><span class="line">                    word_doc[i] +&#x3D; 1</span><br><span class="line">        for i in doc_frequency:</span><br><span class="line">            word_idf[i] &#x3D; math.log(len(list_key) &#x2F; (word_doc[i] + 1))</span><br><span class="line">        # 找到每个词对应文档中出现的次数</span><br><span class="line">        doc_frequency1 &#x3D; defaultdict(int)</span><br><span class="line">        for word_list in list_key:</span><br><span class="line">            for i in word_list:</span><br><span class="line">                doc_frequency1[i] +&#x3D; 1</span><br><span class="line">        # 对样本进行词频统计</span><br><span class="line">        list_doc &#x3D; []</span><br><span class="line">        for i in range(len(list_key)):</span><br><span class="line">            doc_frequency1 &#x3D; defaultdict(int)</span><br><span class="line">            for j in list_key[i]:</span><br><span class="line">                doc_frequency1[j] +&#x3D; 1</span><br><span class="line">            list_doc.append(doc_frequency1)</span><br><span class="line">        # 计算语料库的tf_idf</span><br><span class="line">        tf_idf &#x3D; []</span><br><span class="line">        for j in range(len(data)):</span><br><span class="line">            tf_idf.append([word_idf[i] * list_doc[j][i] &#x2F; len(list_doc[j]) for i in (list_doc[j])])</span><br><span class="line">        # 计算样本的tf-idf得分</span><br><span class="line">        scores &#x3D; []</span><br><span class="line">        for j in range(len(data)):</span><br><span class="line">            score &#x3D; 0</span><br><span class="line">            for i in filtered_sentence:</span><br><span class="line">                score +&#x3D; (word_idf[i] * list_doc[j][i] &#x2F; len(list_doc[j]))</span><br><span class="line">            scores.append(score)</span><br><span class="line">        # 用字典形式排序</span><br><span class="line">        x &#x3D; np.arange(len(data)).tolist()</span><br><span class="line">        dict_score &#x3D; dict(zip(x, scores))</span><br><span class="line">        listc &#x3D; sorted(zip(dict_score.values(), dict_score.keys()))</span><br><span class="line">        recall_ques &#x3D; []</span><br><span class="line">        recall_answ &#x3D; []</span><br><span class="line">        for i in range(1, 6):</span><br><span class="line">            recall_ques.append(question[listc[-i][1]])</span><br><span class="line">            recall_answ.append(answer[listc[-i][1]])</span><br><span class="line">        return recall_ques, recall_answ</span><br></pre></td></tr></table></figure>

<h1 id="求5个问题的相似度"><a href="#求5个问题的相似度" class="headerlink" title="求5个问题的相似度"></a>求5个问题的相似度</h1><p>利用自带的余弦相似性求5个问题和输入问题的相似度</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">def similar_list(text, recall_ques):</span><br><span class="line">    return [nlp(text).similarity(nlp(recall_ques[i])) for i in range(5)]</span><br></pre></td></tr></table></figure>

<h2 id="输出相似度最高的那个问题的答案"><a href="#输出相似度最高的那个问题的答案" class="headerlink" title="输出相似度最高的那个问题的答案"></a>输出相似度最高的那个问题的答案</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">best_answer</span>(<span class="params">list_num, recall_answ</span>):</span></span><br><span class="line">    <span class="comment"># 输出相似度最高的那个的答案</span></span><br><span class="line">    print(recall_answ[list_num.index(<span class="built_in">max</span>(list_num))])</span><br></pre></td></tr></table></figure>

<h2 id="构建无限循环"><a href="#构建无限循环" class="headerlink" title="构建无限循环"></a>构建无限循环</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    data1 = pd.read_csv(<span class="string">&#x27;Q&amp;A pairs.csv&#x27;</span>)</span><br><span class="line">    print(<span class="string">&quot;Hello, I&#x27;m a question-and-answer chatbot for the tourism domain based on retrieval mode.&quot;</span></span><br><span class="line">          <span class="string">&quot;If you want to exit, input &#x27;Bye&#x27;!&quot;</span>)</span><br><span class="line">    greeting_output = [<span class="string">&quot;hi&quot;</span>, <span class="string">&quot;hey&quot;</span>, <span class="string">&quot;hello&quot;</span>, <span class="string">&quot;I&#x27;m glad! You are talking to me&quot;</span>]</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        text2 = <span class="built_in">input</span>(<span class="string">&quot;Please enter a question: \t&quot;</span>)</span><br><span class="line">        text1 = <span class="string">&quot; &quot;</span>.join([token.lemma_ <span class="keyword">for</span> token <span class="keyword">in</span> nlp(text2)])</span><br><span class="line">        <span class="keyword">if</span> text1 == <span class="string">&#x27;hey&#x27;</span> <span class="keyword">or</span> text1 == <span class="string">&#x27;hi&#x27;</span> <span class="keyword">or</span> text1 == <span class="string">&#x27;hello&#x27;</span> <span class="keyword">or</span> text1 == <span class="string">&#x27;HI&#x27;</span>:</span><br><span class="line">            print(random.choice(greeting_output))</span><br><span class="line">        <span class="keyword">elif</span> text1 == <span class="string">&#x27;thank&#x27;</span> <span class="keyword">or</span> text1 == <span class="string">&#x27;thank -PRON-&#x27;</span> <span class="keyword">or</span> text1 == <span class="string">&#x27;THANK&#x27;</span>:</span><br><span class="line">            print(<span class="string">&#x27;You are welcome.&#x27;</span>)</span><br><span class="line">        <span class="keyword">elif</span> text1 == <span class="string">&#x27;bye&#x27;</span> <span class="keyword">or</span> text1 == <span class="string">&#x27;BYE&#x27;</span>:</span><br><span class="line">            print(<span class="string">&#x27;Bye!&#x27;</span>)</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">if</span> QA_tf_idf(data1, del_stop(text1)) == <span class="number">0</span>:</span><br><span class="line">                print(<span class="string">f&quot;I&#x27;m sorry. I don&#x27;t understand you&quot;</span>)</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                recall_ques1, recall_answ1 = QA_tf_idf(data1, del_stop(text1))</span><br><span class="line">                best_answer(similar_list(text1, recall_ques1), recall_answ1)</span><br></pre></td></tr></table></figure>

<p>能够简单实现步骤，下一步加入GUI</p>
<p><img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595262847196&di=b1093ee61bec6c22a07f7fd6b07a168e&imgtype=0&src=http://p2.itc.cn/images01/20200609/12d57ad2d3694d24b69f0db647f43830.jpeg"></p>

    </div>
</article>


                </div>
                <aside class="col-md-4 gal-left" id="sidebar">
    <!-- 此为sidebar的搜索框, 非搜索结果页面 -->
<aside id="sidebar-search">
    <div class="search hidden-xs" data-aos="fade-up" data-aos-duration="2000">
        <form class="form-inline clearfix" id="search-form" method="get"
              action="/search/index.html">
            <input type="text" name="s" class="form-control" id="searchInput" placeholder="搜索文章~" autocomplete="off">
            <button class="btn btn-danger btn-gal" type="submit">
                <i class="fa fa-search"></i>
            </button>
        </form>
    </div>
</aside>
    <aside id="sidebar-author">
    <div class="panel panel-gal" data-aos="flip-right" data-aos-duration="3000">
        <div class="panel-heading" style="text-align: center">
            <i class="fa fa-quote-left"></i>
            esy
            <i class="fa fa-quote-right"></i>
        </div>
        <div class="author-panel text-center">
            <img src="/imgs/avatar.jpg" width="140" height="140"
                 alt="个人头像" class="author-image">
            <p class="author-description"><p>esy</p>
</p>
        </div>
    </div>
</aside>
    
    <aside id="sidebar-recent_comments">
    <div class="panel panel-gal recent hidden-xs" data-aos="fade-up" data-aos-duration="2000">
        <div class="panel-heading">
            <i class="fa fa-comments"></i>
            最新评论
            <i class="fa fa-times-circle panel-remove"></i>
            <i class="fa fa-chevron-circle-up panel-toggle"></i>
        </div>
        <ul class="list-group list-group-flush"></ul>
    </div>
</aside>
    
    <!-- 要配置好leancloud才能开启此小工具 -->
    
    
    <aside id="sidebar-recent_posts">
    <div class="panel panel-gal recent hidden-xs" data-aos="fade-up" data-aos-duration="2000">
        <div class="panel-heading">
            <i class="fa fa-refresh"></i>
            近期文章
            <i class="fa fa-times-circle panel-remove"></i>
            <i class="fa fa-chevron-circle-up panel-toggle"></i>
        </div>
        <ul class="list-group list-group-flush">
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/11/05/python%20work/%E6%9C%80%E5%B0%8F%E4%BA%8C%E4%B9%98%E6%B3%95/">最小二乘法</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/11/05/python%20work/%E7%BB%9F%E8%AE%A1%E5%AD%A6%E4%B9%A0-%E7%AC%AC%E4%B8%80%E7%AB%A0/">统计学习--第一章</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/11/04/python%20work/hello-world/">Hello World</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/11/03/python%20work/%E5%88%86%E7%B1%BB%E6%A8%A1%E5%9E%8B%E7%9A%84%E8%AF%84%E4%BC%B0%E6%8C%87%E6%A0%87/">分类模型的评估指标</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/10/21/python%20work/10-21-%E7%88%AC%E8%99%AB%E5%9F%BA%E7%A1%80/">10-21 爬虫基础</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/09/25/python%20work/CRF%E7%9A%84%E6%95%B4%E4%BD%93%E6%B5%81%E7%A8%8B%E7%BB%93%E6%9E%9C/">CRF的整体流程结果</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/09/25/python%20work/nlp-crf%E6%A8%A1%E5%9E%8B/">nlp_crf模型</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/09/25/python%20work/%E6%95%B0%E5%AD%A6%E5%BB%BA%E6%A8%A1%E9%97%AE%E9%A2%983/">数学建模问题3</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/09/25/python%20work/%E6%95%B0%E5%AD%A6%E5%BB%BA%E6%A8%A1%E9%97%AE%E9%A2%982/">数学建模问题2</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/09/25/python%20work/%E6%95%B0%E5%AD%A6%E5%BB%BA%E6%A8%A1%E9%97%AE%E9%A2%981/">数学建模问题1</a>
                </span>
            </li>
            
        </ul>
    </div>
</aside>
    
    
    <aside id="sidebar-rand_posts">
    <div class="panel panel-gal recent hidden-xs" data-aos="fade-up" data-aos-duration="2000">
        <div class="panel-heading">
            <i class="fa fa-refresh"></i>
            随机文章
            <i class="fa fa-times-circle panel-remove"></i>
            <i class="fa fa-chevron-circle-up panel-toggle"></i>
        </div>
        <ul class="list-group list-group-flush">
            
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/08/08/python%20work/gensim%E4%B8%AD%E7%9A%84tfidf/">gensim中的tfidf</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2019/12/02/python%20work/hexo%E7%9A%84%E4%BD%BF%E7%94%A8/">LV-1.3</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/03/26/python%20work/python-%E5%8F%AF%E8%A7%86%E5%8C%96%E4%B9%8Bseaborn/">python_可视化之seaborn</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/02/26/python%20work/python%E2%80%94%E2%80%94%E5%91%BC%E5%90%B8%E9%9F%B3%E7%89%B9%E5%BE%81/">python</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2019/11/20/python%20work/python%E5%9F%BA%E7%A1%80--%E5%BC%82%E5%B8%B8/">python2</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2019/12/01/python%20work/python%E5%9F%BA%E7%A1%80%E4%B9%8B%E8%AF%BB%E5%8F%96mat%E6%96%87%E4%BB%B6/">LV.0</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2019/12/26/python%20work/%E5%8E%BB%E8%B6%8B%E5%8A%BF%E6%B3%A2%E5%8A%A8%E5%88%86%E6%9E%90DFA/">去趋势波动分析DFA</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/08/19/python%20work/%E5%A4%84%E7%90%86%E5%B9%B6%E7%BB%9F%E8%AE%A1%E4%B8%A4%E4%B8%AA%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%AD%E7%9A%84%E7%9D%A1%E7%9C%A0%E6%A0%87%E7%AD%BE/">处理并统计两个数据库中的睡眠标签</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/09/25/python%20work/%E6%95%B0%E5%AD%A6%E5%BB%BA%E6%A8%A1%E9%97%AE%E9%A2%983/">数学建模问题3</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/07/09/python%20work/HMM%E4%B9%8B%E6%95%B0%E6%8D%AE%E7%9A%84%E5%A4%84%E7%90%86/">HMM之数据的处理</a>
                </span>
            </li>
            
        </ul>
    </div>
</aside>
    
    
    <aside id="gal-sets">
        <div class="panel panel-gal hidden-xs" data-aos="fade-up" data-aos-duration="2000">
            <ul class="nav nav-pills pills-gal">

                
                <li>
                    <a href="/2020/07/20/python%20work/chatbot%E5%9C%A8python%E4%B8%8A%E5%AE%9E%E7%8E%B0/index.html#sidebar-tags" data-toggle="tab" id="tags-tab">热门标签</a>
                </li>
                
                
                <li>
                    <a href="/2020/07/20/python%20work/chatbot%E5%9C%A8python%E4%B8%8A%E5%AE%9E%E7%8E%B0/index.html#sidebar-friend-links" data-toggle="tab" id="friend-links-tab">友情链接</a>
                </li>
                
                
                <li>
                    <a href="/2020/07/20/python%20work/chatbot%E5%9C%A8python%E4%B8%8A%E5%AE%9E%E7%8E%B0/index.html#sidebar-links" data-toggle="tab" id="links-tab">个人链接</a>
                </li>
                
            </ul>
            <div class="tab-content">
                
                <div class="cloud-tags tab-pane nav bs-sidenav fade" id="sidebar-tags">
    
    <a href="/tags/py-study/" style="font-size: 14.808471581060314px;" class="tag-cloud-link">py_study</a>
    
    <a href="/tags/nlp/" style="font-size: 18.039320631108126px;" class="tag-cloud-link">nlp</a>
    
    <a href="/tags/Graduation-work/" style="font-size: 9.307359261100746px;" class="tag-cloud-link">Graduation work</a>
    
    <a href="/tags/work/" style="font-size: 13.065854023395152px;" class="tag-cloud-link">work</a>
    
    <a href="/tags/hexo/" style="font-size: 17.51425991785914px;" class="tag-cloud-link">hexo</a>
    
    <a href="/tags/%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/" style="font-size: 15.488603434539105px;" class="tag-cloud-link">-个人博客搭建</a>
    
    <a href="/tags/malab-%E6%AF%95%E4%B8%9A/" style="font-size: 19.859272951858447px;" class="tag-cloud-link">-malab -毕业</a>
    
    <a href="/tags/python-%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/" style="font-size: 19.44186390467823px;" class="tag-cloud-link">-python -人工智能</a>
    
    <a href="/tags/python/" style="font-size: 13.559690705237482px;" class="tag-cloud-link">python</a>
    
    <a href="/tags/python/" style="font-size: 19.079444455512807px;" class="tag-cloud-link">-python</a>
    
    <a href="/tags/mathematical-modeling/" style="font-size: 12.82099253964963px;" class="tag-cloud-link">mathematical modeling</a>
    
    <a href="/tags/statistical-learning/" style="font-size: 17.535581003377942px;" class="tag-cloud-link">statistical learning</a>
    
</div>
                
                
                <div class="friend-links tab-pane nav bs-sidenav fade" id="sidebar-friend-links">
    
    <li>
        <a href="http://kdays.net/days/" target="_blank">KDays Forum</a>
    </li>
    
    <li>
        <a href="http://www.gal123.com/" target="_blank">绅士导航♂</a>
    </li>
    
    <li>
        <a href="http://www.moe123.com/" target="_blank">萌导航</a>
    </li>
    
</div>
                
                
                <div class="links tab-pane nav bs-sidenav fade" id="sidebar-links">
    
    <li>
        <a href="https://github.com/ZEROKISEKI/" target="_blank">Github</a>
    </li>
    
    <li>
        <a href="https://coding.net/u/SORA1" target="_blank">Coding</a>
    </li>
    
    <li>
        <a href="https://www.zhihu.com/people/aonosora/activities" target="_blank">知乎</a>
    </li>
    
</div>
                
            </div>
        </div>
    </aside>
    
</aside>
            </div>
        </div>
    </div>
    <footer id="gal-footer">
    <div class="container">
        Copyright © 2018 esy Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>.&nbsp;Theme by <a href="https://github.com/ZEROKISEKI" target="_blank">AONOSORA</a>
    </div>
</footer>

<!-- 回到顶端 -->
<div id="gal-gotop">
    <i class="fa fa-angle-up"></i>
</div>
</body>

<script src="/js/activate-power-mode.js"></script>

<script>

    // 配置highslide
	hs.graphicsDir = '/js/highslide/graphics/'
    hs.outlineType = "rounded-white";
    hs.dimmingOpacity = 0.8;
    hs.outlineWhileAnimating = true;
    hs.showCredits = false;
    hs.captionEval = "this.thumb.alt";
    hs.numberPosition = "caption";
    hs.align = "center";
    hs.transitions = ["expand", "crossfade"];
    hs.lang.number = '共%2张图, 当前是第%1张';
    hs.addSlideshow({
      interval: 5000,
      repeat: true,
      useControls: true,
      fixedControls: "fit",
      overlayOptions: {
        opacity: 0.75,
        position: "bottom center",
        hideOnMouseOut: true
      }
    })

    // 初始化aos
    AOS.init({
      duration: 1000,
      delay: 0,
      easing: 'ease-out-back'
    });

</script>
<script>
	POWERMODE.colorful = 'true';    // make power mode colorful
	POWERMODE.shake = 'true';       // turn off shake
	// TODO 这里根据具体情况修改
	document.body.addEventListener('input', POWERMODE);
</script>
<script>
    window.slideConfig = {
      prefix: '/imgs/slide/background',
      ext: 'jpg',
      maxCount: '6'
    }
</script>

<script src="/js/hs.js"></script>
<script src="/js/blog.js"></script>



<script src="/js/oni.js"></script>




</html>